﻿using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Moolink.Helper;
using superSocketApi.DataBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace superSocketApi.Controllers
{
	[EnableCors("cors")]
	[Route("api/[controller]")]
	[ApiController]
	public class DeviceController : ControllerBase
	{
		[HttpGet]
		[Route("List")]
		public async Task<IActionResult> DeviceList()
		{
			return await Task.Run(() => { return Ok(Global.DeviceInfoFun.DeviceList()); });
		}

		//[HttpGet]
		//[Route("devices/DeviceRealTimeData")]
		//public async Task<IActionResult> DeviceRealTimeData()
		//{
		//	return await Task.Run(() => { return Ok(rtDatOpt.GetWbRtDatView()); });
		//}
		//[HttpGet]
		//[Route("devices/DeviceStateLogThisDay")]
		//public async Task<IActionResult> DeviceStateLogThisDay(string manchineId)
		//{
		//	return await Task.Run(() => { return Ok(rtDatOpt.GetWbStateLogDatView(manchineId)); });
		//}

		//SELECT * FROM yaxin.table_wb_statelog where MachineID='IA27-062' and LogTime>='2021-04-22 00:00:00';

		[HttpGet]
		[Route("{deviceid}/Info")]
		public async Task<IActionResult> Device(string deviceid)
		{
			return await Task.Run(() => { return Ok(RedisHelper.GetHashValue("YaXinDevice", deviceid, GlobalSystem.RedisDB_YaXinDevice)); });
		}

		[HttpGet]
		[Route("{deviceid}/CollData")]
		public async Task<IActionResult> GetCollData(string deviceid)
		{
			return await Task.Run(() => { return Ok(RedisHelper.GetStringValue("YaXinCollData:" + deviceid, GlobalSystem.RedisDB_YaXinDevice)); });
		}

		[HttpPost]
		[Route("{deviceid}/SendHsmsData")]
		public async Task<IActionResult> SendHsmsData(ushort deviceid, byte stream, byte function, [FromBody] string bodyData)
		{
			//ushort devid = Convert.ToUInt16(deviceid);
			if (GlobalSystem.deviceIdToSessionId.ContainsKey(deviceid))
			{
				string sessionId = GlobalSystem.deviceIdToSessionId[deviceid];
				return await Task.Run(() => { return Ok(Global.ServerInfoFun.SendHsmsDataBySessionId(sessionId, stream, function, bodyData)); });
			}
			else
			{
				return await Task.Run(() => { return Ok(); });
			}

		}


		[HttpPost]
		[Route("{deviceid}/SendFile")]
		public async Task<IActionResult> SendFile(ushort deviceid, [FromBody] string bodyData)
		{
			if (GlobalSystem.deviceIdToSessionId.ContainsKey(deviceid))
			{
				string sessionId = GlobalSystem.deviceIdToSessionId[deviceid];
				return await Task.Run(() => { return Ok(Global.DeviceInfoFun.SendFileReq(deviceid, sessionId, bodyData)); });
			}
			else
			{
				return await Task.Run(() => { return Ok(); });
			}

		}
		[HttpPost]
		[Route("{deviceid}/GetFile")]
		public async Task<IActionResult> GetFile(ushort deviceid, [FromBody] string bodyData)
		{
			if (GlobalSystem.deviceIdToSessionId.ContainsKey(deviceid))
			{
				string sessionId = GlobalSystem.deviceIdToSessionId[deviceid];
				return await Task.Run(() => { return Ok(Global.DeviceInfoFun.GetFileReq(deviceid, sessionId, bodyData)); });
			}
			else
			{
				return await Task.Run(() => { return Ok(); });
			}

		}

		[HttpPost]
		[Route("{deviceid}/SendCtrlCmd")]
		public async Task<IActionResult> SendCtrlCmd(ushort deviceid, byte cmd, [FromBody] string password)
		{
			//ushort devid = Convert.ToUInt16(deviceid);
			if (GlobalSystem.deviceIdToSessionId.ContainsKey(deviceid))
			{
				string sessionId = GlobalSystem.deviceIdToSessionId[deviceid];
				return await Task.Run(() => { return Ok(Global.DeviceInfoFun.SendCtrlCmd(deviceid, sessionId, cmd, password)); });
			}
			else
			{
				return await Task.Run(() => { return Ok(); });
			}

		}



	}
}
